package com.xydtech.ecard.ecard.controller;

import com.alibaba.fastjson.JSONObject;
import com.xydtech.common.annotation.PassToken;
import com.xydtech.common.enums.ResultStatus;
import com.xydtech.common.model.ResponseModel;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.ecard.config.domain.EcardServeLibraryConfig;
import com.xydtech.ecard.config.domain.EcardServeLibraryNews;
import com.xydtech.ecard.config.service.IEcardServeLibraryConfigService;
import com.xydtech.ecard.config.service.IEcardServeLibraryNewsService;
import com.xydtech.ecard.ecard.domain.EcardApiVisitRecord;
import com.xydtech.ecard.user.domain.EcardApiUserInfo;
import com.xydtech.ecard.user.service.IEcardApiUserService;
import com.xydtech.system.domain.SysWechatApplet;
import com.xydtech.system.service.ISysWechatAppletService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 电子名片-知识文库模块Controller
 *
 * @author yuzhiguo
 * @date 2024-09-18 下午3:09
 */
@Controller
@Api(tags = "电子名片-电子名片-知识文库信息")
@RequestMapping("/ecardSaasApi/ecard/serveLibrary")
public class EcardServeLibraryController extends EcardApiBaseController {
    @Autowired
    private IEcardServeLibraryConfigService ecardServeLibraryConfigService;
    @Autowired
    private ISysWechatAppletService wechatAppletService;
    @Autowired
    private IEcardServeLibraryNewsService ecardServeLibraryNewsService;
    @Autowired
    private IEcardApiUserService ecardApiUserService;

    /**
     * 电子名片-电子名片-获取文库数据
     *
     * @author yuzhiguo
     * @date 2024/9/18 下午3:10
     */
    @PassToken
    @ApiOperation(value = "电子名片-电子名片-获取文库数据", notes = "电子名片-电子名片-获取文库数据")
    @ApiResponses({@ApiResponse(code = 0, message = "OK", response = EcardApiVisitRecord.class)})
    @GetMapping("/findServeLibraryList")
    @ResponseBody
    public ResponseModel findServeLibraryList() {
        String salt = getSalt();//盐值
        String currentUserId = getCurrentUserId();
        EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(currentUserId, salt);
        //当前小程序id
        SysWechatApplet wechatApplet = getAppIdByDeptId(userInfo.getDeptId());
        String appId = wechatApplet.getAppid();
        if (StringUtils.isEmpty(appId)) {
            return ResponseModel.error(ResultStatus.PARAMETER_ERROR);
        }
        //当前小程序对应的法人机构ID
        Long parentDeptId = wechatApplet.getDeptId();

        List<EcardServeLibraryConfig> ecardServeLibraryConfigList = ecardServeLibraryConfigService.findServeLibraryList(parentDeptId);
        return ResponseModel.ok(ecardServeLibraryConfigList);
    }

    /**
     * 电子名片-电子名片-获取文库模块文库信息列表
     *
     * @author yuzhiguo
     * @date 2024/9/18 下午3:22
     */
    @ApiOperation(value = "电子名片-电子名片-获取文库模块文库信息列表", notes = "电子名片-电子名片-获取文库模块文库信息列表")
    @ApiResponses({@ApiResponse(code = 0, message = "OK", response = EcardApiVisitRecord.class)})
    @PostMapping("/findServeLibraryNewsList")
    @ResponseBody
    public ResponseModel findServeLibraryNewsList(@RequestBody String json) {
        JSONObject jsonObject = JSONObject.parseObject(json);
        String libId = jsonObject.getString("libId");//文库表关联id
        String libTitle = jsonObject.getString("libTitle");//文库标题
        if (StringUtils.objIsNullOrEmpty(libId)) {
            return ResponseModel.error(ResultStatus.PARAMETER_MISSING);
        }
        EcardServeLibraryNews ecardServeLibraryNews = new EcardServeLibraryNews();
        ecardServeLibraryNews.setLibId(Long.valueOf(libId));
        ecardServeLibraryNews.setLibTitle(libTitle);
        startPage();
        List<EcardServeLibraryNews> list = ecardServeLibraryNewsService.selectEcardServeLibraryNewsList(ecardServeLibraryNews);
        return getTableResponse(list);
    }

    /**
     * 电子名片-电子名片-获取文库模块文库信息详情
     *
     * @author yuzhiguo
     * @date 2024/9/18 下午3:35
     */
    @PassToken
    @ApiOperation(value = "电子名片-电子名片-获取文库模块文库信息详情", notes = "电子名片-电子名片-获取文库模块文库信息详情")
    @ApiResponses({@ApiResponse(code = 0, message = "OK", response = EcardApiVisitRecord.class)})
    @PostMapping("/findServeLibraryNewsInfo")
    @ResponseBody
    public ResponseModel findServeLibraryNewsInfo(@RequestBody String json) {
        JSONObject jsonObject = JSONObject.parseObject(json);
        String id = jsonObject.getString("id");//文库信息主键id
        if (StringUtils.isEmpty(id)) {
            return ResponseModel.error(ResultStatus.PARAMETER_ERROR);
        }
        EcardServeLibraryNews ecardServeLibraryNews = ecardServeLibraryNewsService.selectEcardServeLibraryNewsById(Long.valueOf(id));
        return ResponseModel.ok(ecardServeLibraryNews);
    }
}
